package com.papercut.purchase;

import java.util.List;
import java.util.Vector;

import com.papercut.R;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class PurchaseListAdapter extends BaseAdapter {
	private List<PurchaseItem> items;
	private LayoutInflater layoutInflater;
	private Vector<View> listItems;

	public PurchaseListAdapter(LayoutInflater layoutInflater,
			List<PurchaseItem> items) {

		this.layoutInflater = layoutInflater;
		this.items = items;
		this.listItems = new Vector<View>();
	}

	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public Object getItem(int position) {

		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		PurchaseListItemParts purchaseListItemParts;
		PurchaseItem conversation = items.get(position);
		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.purchase_list_item,
					null);
			purchaseListItemParts = new PurchaseListItemParts();
			purchaseListItemParts.title = (TextView) convertView
					.findViewById(R.id.title);
			purchaseListItemParts.price = (TextView) convertView
					.findViewById(R.id.price);

			purchaseListItemParts.purchaseItem = conversation;
			convertView.setTag(purchaseListItemParts);
		} else {
			purchaseListItemParts = (PurchaseListItemParts) convertView
					.getTag();
		}

		purchaseListItemParts.title.setText(conversation.title);
		purchaseListItemParts.price.setText(""+conversation.price);

		if (listItems.size() < getCount()) {
			listItems.setSize(getCount());
		}
		listItems.setElementAt(convertView, position);
		return convertView;
	}

	public View getItemView(int position) {
		return position >= listItems.size() ? null : listItems
				.elementAt(position);
	}

	@Override
	public boolean areAllItemsEnabled() {
		return false;
	}

	@Override
	public boolean isEnabled(int position) {
		return false;
	}

}
